package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonStudentStatisBean;
import com.fancy.learncenter.bean.QuestionVOsBean;
import com.fancy.learncenter.bean.StudentWorkAnsDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.ActivityStudentWorkAdapter;
import com.fancy.learncenter.ui.adapter.CartoonStudentWorkListenPicListAdapter;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonStudentWorkDetailActivity extends BaseActivity {


    String studentHomeworkId;
    String workName;
    String studentName;
    String accuracy;

    ArrayList<String> questionIds;
    @Bind(R.id.user_name_text)
    TextView userNameText;
    @Bind(R.id.right_accurate)
    TextView rightAccurate;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    ActivityStudentWorkAdapter studentWorkAdapter;

    private ArrayList<QuestionVOsBean> questionVOs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_student_work_detail);
        ButterKnife.bind(this);
        workName = getIntent().getStringExtra("workName");
        studentName = getIntent().getStringExtra("studentName");
        studentHomeworkId = getIntent().getStringExtra("studentHomeworkId");
        accuracy = getIntent().getStringExtra("accuracy");
        initToolbar(workName);
        questionIds = getIntent().getStringArrayListExtra("questionIds");

        questionVOs = getIntent().getParcelableArrayListExtra("questionVOs");

        LogUtil.MyLog("getStaticsReport", "========" + questionVOs.size());
        userNameText.setText(studentName);

        rightAccurate.setText(accuracy);

        initView();

        getStaticsReport();


    }

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        studentWorkAdapter = new ActivityStudentWorkAdapter(this, new ArrayList<StudentWorkAnsDataBean.ResultBean>());
        recyclerView.setAdapter(studentWorkAdapter);
    }

    private ArrayList<QuestionVOsBean> questionWorkVOs = new ArrayList<>();

    private void getStaticsReport() {

        Map<String, Object> map = new HashMap<>();

        String[] strings = questionIds.toArray(new String[questionIds.size()]);

        map.put("studentHomeworkId", studentHomeworkId);
        map.put("questionIds", strings);

        HttpMehtod.getInstance().cartoonlistStudentHomeworkAnswer(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<StudentWorkAnsDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), StudentWorkAnsDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<StudentWorkAnsDataBean>() {
                    @Override
                    public void success(StudentWorkAnsDataBean dataBean) {

                        studentWorkAdapter.notifyDataSetChanged(dataBean.getResult());

                        for (int i = 0; i < questionVOs.size(); i++) {
                            for (StudentWorkAnsDataBean.ResultBean resultBean : dataBean.getResult()) {
                                if (resultBean.getQuestionId() == (questionVOs.get(i).getQuestionId())) {
                                    questionWorkVOs.add(questionVOs.get(i));
                                }
                            }
                        }
                        studentWorkAdapter.setQuestionVOs(questionWorkVOs);
                        LogUtil.MyLog("getStaticsReport", "========" + questionWorkVOs.size());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });
    }
}
